package com.jz.netthings.analysis.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.analysis.bean.operManage.FrequencySupport;
import com.jz.netthings.analysis.bean.operManage.ProviderProcess;
import com.jz.netthings.analysis.bean.operManage.PulpingFrequency;
import com.jz.netthings.analysis.bean.operManage.QueryFreQuencySupport;
import com.jz.netthings.analysis.mapper.ProviderProcessMapper;
import com.jz.netthings.analysis.service.ProviderProcessService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.application.bodyCheck.bean.BodyCheck;
import com.jz.netthings.application.bodyCheck.mapper.mysql.BodyCheckMapper;
import com.jz.netthings.application.heatseal.bean.HeatSeal;
import com.jz.netthings.application.heatseal.mapper.mysql.HeatSealMapper;
import com.jz.netthings.application.providerAllot.bean.ProviderAllot;
import com.jz.netthings.application.providerAllot.mapper.mysql.ProviderAllotMapper;
import com.jz.netthings.application.providerCollection.bean.ProviderCollection;
import com.jz.netthings.application.providerCollection.mapper.mysql.ProviderCollectionMapper;
import com.jz.netthings.application.providerCost.bean.ProviderCost;
import com.jz.netthings.application.providerCost.mapper.mysql.ProviderCostMapper;
import com.jz.netthings.application.providerCreate.mapper.mysql.ProviderXpMapper;
import com.jz.netthings.application.providerReg.bean.ProviderReg;
import com.jz.netthings.application.providerReg.mapper.mysql.ProviderRegMapper;
import com.jz.netthings.application.sample.mapper.PlasmaSamplesMapper;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.utils.AdscmDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 浆员献浆过程各环节数量统计图 服务实现类
 * </p>
 *
 * @author yangyunyun
 * @since 2019-07-22
 */
@Service
public class ProviderProcessServiceImpl extends ServiceImpl<ProviderProcessMapper, ProviderProcess> implements ProviderProcessService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ProviderProcessMapper providerProcessMapper;
    @Autowired
    ProviderRegMapper providerRegMapper;
    @Autowired
    BodyCheckMapper bodyCheckMapper;
    @Autowired
    ProviderAllotMapper providerAllotMapper;
    @Autowired
    ProviderXpMapper providerXpMapper;
    @Autowired
    ProviderCollectionMapper providerCollectionMapper;
    @Autowired
    ProviderCostMapper providerCostMapper;
    @Autowired
    HeatSealMapper heatSealMapper;

    @Override
    public ProviderProcess loadProviderProcessByTime(String dateTime) throws AdscmSystemException {

        logger.info("构造【"+dateTime+"】的献浆过程各环节数量统计数据");

        ProviderProcess process = new ProviderProcess();
        Date dt = AdscmDateUtils.parseDateL(dateTime);
        String date = AdscmDateUtils.getNowOnlyDateFomat1(dt);

        process.setTime(dateTime);
        process.setDate(date);

        String beginTime = date + " 00:00:00";
        String endTime = date + " 23:59:59";

        //登记
        Wrapper<ProviderReg> regWrapper = new EntityWrapper<>();
        regWrapper.between("regTime",beginTime,endTime);
        int regCount = providerRegMapper.selectCount(regWrapper);
        process.setRegister(regCount+"");
        //体检
        Wrapper<BodyCheck> bcWrapper = new EntityWrapper<>();
        bcWrapper.between("recodeCreateTime",beginTime,endTime);
        bcWrapper.eq("recodeStatus","1");
        int bcCount = bodyCheckMapper.selectCount(bcWrapper);
        process.setBodyCheck(bcCount+"");
        //分配
        Wrapper<ProviderAllot> allotWrapper = new EntityWrapper<>();
        allotWrapper.between("allotDate",beginTime,endTime);
        int allotCount = providerAllotMapper.selectCount(allotWrapper);
        process.setAllot(allotCount+"");
        //beforeCollection 采浆前
        process.setRegister(allotCount+"");

        //采浆后
        Wrapper<ProviderCollection> collWrapper = new EntityWrapper<>();
        collWrapper.between("collectionDate",beginTime,endTime);
        collWrapper.eq("isDeal","1");
        int collCount = providerCollectionMapper.selectCount(collWrapper);
        process.setAfterCollection(collCount+"");
        //费用
        Wrapper<ProviderCost> costWrapper = new EntityWrapper<>();
        costWrapper.between("createTime",beginTime,endTime);
        costWrapper.eq("state","1");
        int costCount = providerCostMapper.selectCount(costWrapper);
        process.setCost(costCount+"");
        //热合
        Wrapper<HeatSeal> hsWrapper = new EntityWrapper<>();
        hsWrapper.between("createTime",beginTime,endTime);
        hsWrapper.isNotNull("plasmaNo");
        hsWrapper.isNotNull("PlasmaScanTime");
        int hsCount = heatSealMapper.selectCount(hsWrapper);
        process.setHeat(hsCount+"");

        providerProcessMapper.insert(process);

        return process;
    }


    @Override
    public PulpingFrequency pulpingFrequency(String date,Integer firstBlood,Integer secondBlood,Integer thirdBlood,Integer fourthBlood) throws AdscmSystemException {

        logger.info("查询当天进站浆员的献浆频次饼图。。。");

        PulpingFrequency frequency = new PulpingFrequency();
        date = "2019-07-03";//演示用

        String beginTime = date + " 00:00:00";
        String endTime = date + " 23:59:59";
        Wrapper<ProviderReg> regWrapper = new EntityWrapper<>();
        regWrapper.between("regTime",beginTime,endTime);
        List<ProviderReg> regs = providerRegMapper.selectList(regWrapper);

        // FirstBlood=14&SecondBlood=28&ThirdBlood=42&FourthBlood=56

        List<QueryFreQuencySupport> list = providerProcessMapper.selectRegCreateByDate(beginTime, endTime);

        int thisYearjd = 0;//今年建档人数
        FrequencySupport freThisYear = new FrequencySupport();//今年建档人数的频次详情

        int lastYearjd = 0;//去年建档人数
        FrequencySupport freLastYear = new FrequencySupport();//去年建档人数的频次详情

        int beforeYearjd = 0;//前年建档人数
        FrequencySupport freBeforeYear = new FrequencySupport();//前年建档人数的频次详情

        int otherYearjd = 0;//其他建档人数
        FrequencySupport freOtherYear = new FrequencySupport();//其他建档人数的频次详情

        int thisYear = AdscmDateUtils.getTheYear(new Date());
        if (list != null && list.size()>0){
            for (QueryFreQuencySupport freq : list) {
                Date createDate = freq.getCreateDate();
                Date regTime = freq.getRegTime();
                Date lastCollDate = freq.getLastCollDate();

                //献浆间隔天数
                int betweenDays = 0;
                if(regTime != null && lastCollDate != null){
                    betweenDays = AdscmDateUtils.daysBetween(lastCollDate,regTime);
                }

                //建档年份
                int createYear = AdscmDateUtils.getTheYear(createDate);
                if (createYear == thisYear){
                    thisYearjd++;
                    this.setBetweenDaysToSupport(freThisYear,betweenDays,firstBlood,secondBlood,thirdBlood,fourthBlood);
                }else if(createYear == (thisYear-1)){
                    lastYearjd++;
                    this.setBetweenDaysToSupport(freLastYear,betweenDays,firstBlood,secondBlood,thirdBlood,fourthBlood);
                }else if(createYear == (thisYear-2)){
                    beforeYearjd++;
                    this.setBetweenDaysToSupport(freBeforeYear,betweenDays,firstBlood,secondBlood,thirdBlood,fourthBlood);
                }else{
                    otherYearjd++;
                    this.setBetweenDaysToSupport(freOtherYear,betweenDays,firstBlood,secondBlood,thirdBlood,fourthBlood);
                }

            }
        }

        frequency.setThisYearjd(thisYearjd);
        frequency.setFreThisYear(freThisYear);
        frequency.setLastYearjd(lastYearjd);
        frequency.setFreLastYear(freLastYear);
        frequency.setBeforeYearjd(beforeYearjd);
        frequency.setFreBeforeYear(freBeforeYear);
        frequency.setOtherYearjd(otherYearjd);
        frequency.setFreOtherYear(freOtherYear);

        return frequency;
    }

    private void setBetweenDaysToSupport(FrequencySupport support,Integer betweenDays,Integer firstBlood,Integer secondBlood,Integer thirdBlood,Integer fourthBlood){

        if (firstBlood == null){
            firstBlood = 14;
        }
        if (secondBlood == null){
            secondBlood = 28;
        }
        if (thirdBlood == null){
            thirdBlood = 42;
        }
        if (fourthBlood == null){
            fourthBlood = 56;
        }

        int day1 = support.getXjBetweenDay1PCount();
        int day2 = support.getXjBetweenDay2PCount();
        int day3 = support.getXjBetweenDay3PCount();
        int day4 = support.getXjBetweenDay4PCount();
        int day5 = support.getXjBetweenDay5PCount();
        if (betweenDays <= firstBlood){
            day2 ++;
        }else if(betweenDays > firstBlood && betweenDays <= secondBlood){
            day2 ++;
        }else if(betweenDays > secondBlood && betweenDays <= thirdBlood){
            day3 ++;
        }else if(betweenDays > thirdBlood && betweenDays <= fourthBlood){
            day4 ++;
        }else if(betweenDays > fourthBlood){
            day5 ++;
        }
        support.setXjBetweenDay1PCount(day1);
        support.setXjBetweenDay2PCount(day2);
        support.setXjBetweenDay3PCount(day3);
        support.setXjBetweenDay4PCount(day4);
        support.setXjBetweenDay5PCount(day5);
    }

}
